package com.blu.controller;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.blu.job.JobCount;
import com.blu.job.invalidlist;
import com.blu.job.parselist;
import com.blu.jsoup.HtmlParseUtil;

@Controller
public class IndexController {
	
	@GetMapping("/")
	public String index() {
		return "index";
	}
	
	@GetMapping("toadd")
	public String toAdd() {
		return "add";
	}
	
	@GetMapping("toremove")
	public String toRemove() {
		return "remove";
	}
	
	@GetMapping("list")
	public String toList(Model model) {
		model.addAttribute("list",JobCount.list);
		return "list";
	}
	
	@GetMapping("invalid")
	public String toInvalid(Model model) {
		model.addAttribute("list",invalidlist.list);
		return "invalid";
	}
	
	@PostMapping("parse")
	public String parse2(Model model,@RequestParam String username) throws MalformedURLException, IOException {
		parselist.list.clear();
		HtmlParseUtil.parse(username);
		ArrayList<String> templist = new ArrayList<String>();
		for(String url : parselist.list) {
			if(!JobCount.list.contains(url)) {
				templist.add(url);
			}
		}
		parselist.list.clear();
		parselist.list = templist;
		templist = null;
		model.addAttribute("list",parselist.list);
		model.addAttribute("read",parselist.read);
		model.addAttribute("username",username);
		return "parse";
	}

}
